package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import controleur.SudokuController;


public class SudokuFrame extends JFrame
{
	private static final long serialVersionUID = 1L;
	static public final String MENU_ACTION_FILE_QUIT = "Quit";
	static public final String MENU_ACTION_GRID_LOAD = "Load";
    static public final String MENU_ACTION_GRID_SOLVE = "Solve";
    static public final String MENU_ACTION_HELP_ABOUT = "About";
    SudokuController.MenuActionListener listener;
    
    private JFileChooser fc; 
   
    private GridPanel gridPanel;
    
    /*public static void main(String [] args) throws IOException
    {
    	SudokuGridMaker model = new SudokuGridMaker();
    	
		int [][] sgrid = model.makeSudokuGrid();
    	
    	SudokuFrame frame = new SudokuFrame();
    	frame.setGrid(sgrid);
        frame.setVisible(true);
    }*/
    
    public SudokuFrame()
    {
        super("LOG320 - Lab02 - Sudoku");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        fc = new JFileChooser();
        fc.setCurrentDirectory(new File("."));
        
        init();
        
        setSize(375, 400);
        setVisible(true);
    }
    
    public void setMenuActionListener(SudokuController.MenuActionListener list)
    {
    	this.listener = list;
    	initMenu();
    }
    
    private void init()
    {
	   initMenu();
    }
    
    public void updateGridPanel(int [][] grid)
    {
    	if (this.gridPanel == null)
    		JOptionPane.showMessageDialog(this, "There is no grid to solve");
    	else
    		this.gridPanel.updateGridCells(grid);
    }
    
    public void setGrid(int [][] grid)
    {
    	if (this.gridPanel == null)
    	{
    		this.gridPanel = new GridPanel(grid);
    		add( gridPanel, BorderLayout.CENTER );
    	}
    	
    	repaint();
    	setVisible(true);
    }
    
    /*public void setGrid(int [][] grid, boolean isUpdate)
    {
    	if (this.gridPanel == null)
    	{
    		this.gridPanel = new GridPanel(grid);
    		add( gridPanel, BorderLayout.CENTER );
    	}
    	else if (isUpdate)
    	{
    		this.gridPanel.updateGridCells(grid);
    	}
    	else
    	{
    		this.gridPanel.init(grid);
    	}
    	
    	repaint();
    	setVisible(true);
    }*/
    
    private void initMenu()
    {
	   JMenuBar menuBar = new JMenuBar();
	   
	   JMenu mnuFile = new JMenu("File");
	   mnuFile.add( createJMenu("Quit", MENU_ACTION_FILE_QUIT, "alt X", listener) );
	   menuBar.add(mnuFile);
	   
	   JMenu mnuGrid = new JMenu("Grid");
	   mnuGrid.add( createJMenu("Load grid", MENU_ACTION_GRID_LOAD, "ctrl O", listener) );
	   mnuGrid.addSeparator();
	   mnuGrid.add( createJMenu("Solve grid", MENU_ACTION_GRID_SOLVE, "ctrl R", listener) );
	   menuBar.add(mnuGrid);
	   
	   JMenu mnuAbout = new JMenu("About");
	   mnuAbout.add( createJMenu("About", MENU_ACTION_HELP_ABOUT, "ctrl A", listener) );
	   menuBar.add(mnuAbout);
	   
	   this.setJMenuBar(menuBar);
    }
    
    public void quit()
    {
    	System.exit(0);
    }
	 
    public void showAbout()
    {
    	
    }
	   
       private JMenuItem createJMenu(String caption, String command, String accelerator, ActionListener listener) { 
           return createJMenu(caption, command, accelerator, listener, false, false);
       }
       
       private JMenuItem createJMenu(String caption, String command, String accelerator, ActionListener listener, boolean checkedMenu, boolean selected) {
           JMenuItem mnuItem;
           if (checkedMenu) {
               mnuItem = new JCheckBoxMenuItem(caption);
               ((JCheckBoxMenuItem) mnuItem).setSelected(selected);
           } else {
               mnuItem = new JMenuItem(caption);
           }
           if (null != command) {
               mnuItem.setActionCommand(command);
           }
           if (null != accelerator) {
               mnuItem.setAccelerator( KeyStroke.getKeyStroke(accelerator) );
           }
           if (null != listener) {
               mnuItem.addActionListener(listener);
           }
           return mnuItem;
       }
       
}
